package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig{

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue directQueue(){
        return new Queue(DIRECT_QUEUE);
    }

    @Bean
    public Binding bindDirectQueue(){
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }
    @Bean
    public DirectExchange dlExchange(){
        return new DirectExchange("dl.exchange");
    }
    @Bean
    public Queue dlQueue(){
        return QueueBuilder.durable("dl.queue")
                .deadLetterExchange("ttl.exchange")
                .deadLetterRoutingKey("ttl.key")
                .ttl(20000)
                .build();
    }
    @Bean
    public Binding bindDlQueue(){
        return BindingBuilder.bind(dlQueue()).to(dlExchange()).with("dl.key");
    }

    /**
     * 惰性队列
     * @return
     */
  @Bean
   public Queue lazyQueue(){
        return QueueBuilder.durable("lazy.queue1")
                .lazy()
                        .build();
   }
}
